﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace WeManageService
{
	public class FileManager : IXmlSerializable
	{
		public static int PlanNextID { get; set; }
		public static int WishNextID { get; set; }
		public static int TurnoverNextID { get; set; }
		public static List<DateTime> NextTime { get; set; }

		private static string readFlag;

		public static void SetDefault()
		{
			//此处设置首次运行时的默认值
			PlanNextID = WishNextID = TurnoverNextID = 1;
			NextTime = new List<DateTime>();
			for (int i = 0; i < 3; i++)
			{
				NextTime.Add(DateTime.Now);
			}
		}

		public void ReadXml(XmlReader reader)
		{
			try
			{
				//先读取<FileManager>
				reader.Read();

				//读取PlanManager中的NextID
				reader.ReadStartElement("PlanNextID");
				PlanNextID = (int)new XmlSerializer(typeof(int)).Deserialize(reader);
				reader.ReadEndElement();

				//读取WishManager中的NextID
				reader.ReadStartElement("WishNextID");
				WishNextID = (int)new XmlSerializer(typeof(int)).Deserialize(reader);
				reader.ReadEndElement();

				//读取TurnoverManager中的NextID
				reader.ReadStartElement("TurnoverNextID");
				TurnoverNextID = (int)new XmlSerializer(typeof(int)).Deserialize(reader);
				reader.ReadEndElement();

				//读取DelayRecorder中的NextTime
				reader.ReadStartElement("NextTime");
				NextTime = (List<DateTime>)new XmlSerializer(typeof(List<DateTime>)).Deserialize(reader);
				reader.ReadEndElement();

				//在此处添加新的需要存储的值

				//最后读取</FileManager>
				reader.ReadEndElement();
			}
			catch (Exception ex) { readFlag = ex.Message; }
		}

		public void WriteXml(XmlWriter writer)
		{
			//写入PlanManager中的NextID
			writer.WriteStartElement("PlanNextID");
			new XmlSerializer(typeof(int)).Serialize(writer, PlanNextID);
			writer.WriteEndElement();

			//写入WishManager中的NextID
			writer.WriteStartElement("WishNextID");
			new XmlSerializer(typeof(int)).Serialize(writer, WishNextID);
			writer.WriteEndElement();

			//写入TurnoverManager中的NextID
			writer.WriteStartElement("TurnoverNextID");
			new XmlSerializer(typeof(int)).Serialize(writer, TurnoverNextID);
			writer.WriteEndElement();

			//写入DelayRecorder中的NextTime
			writer.WriteStartElement("NextTime");
			new XmlSerializer(typeof(List<DateTime>)).Serialize(writer, NextTime);
			writer.WriteEndElement();

			//在此处添加新的需要存储的值
		}

		public XmlSchema GetSchema() => null;

		public static string Read()
		{
			XmlSerializer reader = new XmlSerializer(typeof(FileManager));
			FileStream fsin;
			try { fsin = new FileStream("data.xml", FileMode.Open); }
			catch (Exception e) { SetDefault(); return e.Message; }
			readFlag = "";
			try { reader.Deserialize(fsin); }
			catch (Exception e) { SetDefault(); return e.Message; }
			finally { fsin.Close(); }
			if (readFlag == "") return "Read successful";
			else { SetDefault(); return readFlag; }
		}

		public static void Write()
		{
			XmlSerializer writer = new XmlSerializer(typeof(FileManager));
			FileStream fsout = new FileStream("data.xml", FileMode.Create);
			writer.Serialize(fsout, new FileManager());
			fsout.Close();
		}
	}
}
